import { request, downloadFile } from '../../tool/HttpRequest'
import { saveFile, FileSystemManager,InnerAudioContext } from '../../tool/wx.public'
let AudioContext = null
Page({

    /**
     * 页面的初始数据
     */
    data: {
        localitypath: '',
        audioList:[],
        playIndex:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(wx.getWindowInfo().statusBarHeight)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    requests() {
        // console.log('requestsrequests')

        // var text = "Hello, world!";
        // var key = CryptoJS.lib.WordArray.random(16);
        // this.decryptionCryptoJS(this.encryptionCryptoJS('selfsummer'))
        // console.log(this.encryptionCryptoJS(text))

        // console.log(this.decryptionCryptoJS('1DFE2C53CFB2DC59E62ACF930F645424'),'-------------');

        // request('https://apis.map.qq.com/ws/geocoder/v1/?location=39.98210863924864,116.31310899739151&key=KENBZ-RAKRI-3CNGU-5M7OL-6Y56O-BMBDH&output=rgeoc&get_poi=3')
        // request('https://apis.map.qq.com/ws/geocoder/v1/?location=39.98210863924864,116.31310899739151&key=KENBZ-RAKRI-3CNGU-5M7OL-6Y56O-BMBDH&output=rgeoc&get_poi=1')


        request('http://rwapitest.cheeseword.com/v1/CourseStudy/dialogueTxt', { uid: 1763, section_id: 18 }).then(uncompressedData => {
            const dwaudio = uncompressedData.dialogue
            if (Array.isArray(dwaudio)) {
                dwaudio.forEach(async item => {
                    const adimg = await downloadFile(`https://rwstatic.tentons.cn/${item.img}`).catch(err => {
                        console.log(err, '错误--adimg')
                    })
                    
                    item.old_img = item.img
                    item.img = adimg.LocalAddress
                    // console.log('img', adimg);

                    const uk_voice = await downloadFile(`https://rwstatic.tentons.cn/${item.uk_voice}`).catch(err => {
                        console.log(err, '错误--uk_voice')
                    })
                    item.old_uk_voice = item.uk_voice
                    item.uk_voice = uk_voice.LocalAddress
                    
                    // console.log('uk_voice', uk_voice);
                    const us_voice = await downloadFile(`https://rwstatic.tentons.cn/${item.us_voice}`).catch(err => {
                        console.log(err, '错误--us_voice')
                    })
                    item.old_us_voice = item.us_voice
                    item.us_voice = us_voice.LocalAddress
                    
                    // console.log('us_voice', us_voice);
                })
                // console.log(JSON.stringify(dwaudio), 'dwaudiodwaudio');
                this.setData({
                    audioList:dwaudio
                })
            }
            // console.log(uncompressedData.dialogue, '----------')
        }).catch(err=>{
            console.log(err)
        })
        // https://audio04.dmhmusic.com/71_53_T10038818736_128_4_4_0_sdk-cpm/cn/0101/M00/0D/18/ChR45F8hIlaAaMO7AAdYKhohZxY895.mp3?xcode=9acfeb989fef601ecd39f04d5425b03455a4ab6
        // https://rwstatic.tentons.cn/voice/course/en_uk/E4D16F877EBE6F4388F682842A76686C.wav
        // downloadFile('https://audio04.dmhmusic.com/71_53_T10064300267_128_4_1_0_sdk-cpm/cn/0513/M00/D1/8C/ChAKCGU3b8WABLA7AFB_3JMpauw322.mp3?xcode=171165dbc9ac8badcdb905e85e2277f1b2b7f1e')
        // downloadFile('https://audio04.dmhmusic.com/71_53_T10064300267_128_4_1_0_sdk-cpm/cn/0513/M00/D1/8C/ChAKCGU3b8WABLA7AFB_3JMpauw322.mp3?xcode=171165dbc9ac8badcdb905e85e2277f1b2b7f1e')
        /**
         * voice/course/en_us/E4D16F877EBE6F4388F682842A76686C.wav
         * voice/course/en_uk/34C87CA21FEC2CF391FBF164000BBDE5T.wav?v=5389
         * voice/course/en_us/34C87CA21FEC2CF391FBF164000BBDE5.wav
         * voice/course/en_uk/57C39EDFA88F6431636605F4B77F6C68.wav
         * voice/course/en_us/57C39EDFA88F6431636605F4B77F6C68.wav
         * voice/course/en_uk/2861126E49F9801841F92699C7589D28T.wav?v=6695
         * voice/course/en_us/2861126E49F9801841F92699C7589D28.wav
         */

        // downloadFile('https://rwstatic.tentons.cn/voice/course/en_us/2861126E49F9801841F92699C7589D28.wav')
        // downloadFile('https://rwstatic.tentons.cn/voice/course/en_uk/2861126E49F9801841F92699C7589D28T.wav')
        // downloadFile('https://rwstatic.tentons.cn/voice/course/en_uk/57C39EDFA88F6431636605F4B77F6C68.wav')
        // downloadFile('https://rwstatic.tentons.cn/voice/course/en_us/57C39EDFA88F6431636605F4B77F6C68.wav')
        downloadFile('https://rwstatic.tentons.cn/voice/course/en_us/57C39EDFA88F6431636605F4B77F6C68.wav').then(res => {
            console.log(res, '本地播放地址');
            this.setData({ localitypath: res.SaveFIleAddress })
            // saveFile(res)
        })

        // request('http://rwapitest.cheeseword.com/v1/CourseStudy/oralImitation')
        //   console.log( EncryptionToDecrypt.decryptionCryptoJS('1DFE2C53CFB2DC59E62ACF930F645424'))
        //   console.log(EncryptionToDecrypt.decryptionCryptoJS(EncryptionToDecrypt.encryptionCryptoJS('的后果撒娇的哈sdaddd56485【【】、、')))

    },
    openmkdir() {
        const fs = wx.getFileSystemManager();
        fs.readdir({
            dirPath: getApp().globalData.downloadFileAddress,
            success({ errMsg, files }) {
                if (errMsg === 'readdir:ok') {
                    console.log(errMsg, files);
                    return files
                }
            },
            fail(err) {
                console.log('fail', err)
            }
        })
    },
    getfile() {
        const app = getApp();
        console.log(app, '--------------')
        FileSystemManager.readdir(`${wx.env.USER_DATA_PATH}/111`).then(res=>{
            console.log(res)
        })
    },
    PlayAudio(){
        if(AudioContext === null) {
            AudioContext =  new InnerAudioContext(this.data.audioList[0].uk_voice)
            AudioContext.breakpoint()
        }else{
            AudioContext.breakpoint()
            // AudioContext.innerAudioContext.destroy()
        }
      
    },
    PlayAudios(){
        AudioContext =  new InnerAudioContext(this.data.audioList[1].uk_voice)
        this.data.playIndex !==0 &&  this.Playstop()
        // AudioContext.stop()
        // setTimeout(() => {
            AudioContext.play()
            this.setData({
                playIndex:++this.data.playIndex
            })
        // }, 1000);

    },
    Playstop(){
        AudioContext.stop()
    },
})